#coding=utf-8
#coding=gbk 

'''
Created on 2015��2��21��

@author: mango
'''
import urllib
import httplib
import json
import os
import os.path
import urlparse
import time
import random
import re
import urllib
import urllib2
import logging_save

class Play_pasuse_next_pre(object):
    def __init__(self,conn=httplib.HTTPConnection('192.168.1.102'),headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"}):
        self.Connection=conn  
        self.Headers=headers   
    
    def pause(self):
        logging_save.logging.info('播放歌曲将会处于暂停状态，请确认....') 
        
        postbody_pause='{"command":"key","value":9}'
        self.Connection.request('POST', '/api/key',postbody_pause,self.Headers)
        self.Connection.close()
        
        self.Connection.request('GET', '/api/request-dev')
        getinformation=self.Connection.getresponse()
        readinformation=getinformation.read()
        getdeviceinfo=eval(readinformation)
        currentstatus=getdeviceinfo['p_status']
        
        self.Connection.close()
        time.sleep(4)
        if currentstatus==2:
            #print 'Result:播放处于暂停状态'
            logging_save.logging.info('Result:播放处于暂停状态') 
        else:
            logging_save.logging.warning('FAIL,FAIL,FAIL,播放未处于暂停状态') 
            
        time.sleep(5)
        
        logging_save.logging.info('恢复播放,可以正常playback歌曲，请确认....')
        
        #print '恢复播放,可以正常playback歌曲，请确认....'
        postbody_play='{"command":"key","value":1}'  
        self.Connection.request('POST', '/api/key',postbody_play,self.Headers)
        self.Connection.close()
        time.sleep(4)
        self.Connection.request('GET', '/api/request-dev')
        getinformation_2=self.Connection.getresponse()
        readinformation_2=getinformation_2.read()
        #print readinformation_2
        getdeviceinfo_2=eval(readinformation_2)
        currentstatus_2=getdeviceinfo_2['p_status']
        self.Connection.close()
        if currentstatus_2==1:
            logging_save.logging.info('Result:处于播放状态')
            #print 'Result:处于播放状态'
        else:
            logging_save.logging.warning('FAIL,FAIL,FAIL,未处于正常播放状态') 
            #print 'Fail，未处于正常播放状态'
    
            
    def next_music(self):
        logging_save.logging.info('切换到下一首曲目，请等待。。。')
        #print '切换到下一首曲目，请等待。。。'
        postbody_next='{"command":"key","value":7}'  
        self.Connection.request('POST', '/api/key',postbody_next,self.Headers)
        self.Connection.close()
        self.Connection.request('GET', '/api/request-dev')
        getinformation_3=self.Connection.getresponse()
        readinformation_3=getinformation_3.read()
        #print readinformation_3
        time.sleep(5)
        
        getdeviceinfo_3=eval(readinformation_3)
        currentstatus_name=getdeviceinfo_3['name']
        currentstatus_artist=getdeviceinfo_3['artist']
        currentstatus_album=getdeviceinfo_3['album']
        currentstatus_url=getdeviceinfo_3['url']
        
        #先返回到上一步，然后再next到原来曲目，然后对比Get获取的关键信息
        postbody_before='{"command":"key","value":8}'  
        self.Connection.request('POST', '/api/key',postbody_before,self.Headers)
        self.Connection.close()
        time.sleep(5)
        
        postbody_before='{"command":"key","value":7}'  
        self.Connection.request('POST', '/api/key',postbody_before,self.Headers)
        self.Connection.close()
        time.sleep(5)
        
        self.Connection.request('GET', '/api/request-dev')
        getinformation_4=self.Connection.getresponse()
        readinformation_4=getinformation_4.read()
        #print readinformation_4
        
        getdeviceinfo_4=eval(readinformation_4)
        currentstatus_name_cmp=getdeviceinfo_4['name']
        currentstatus_artis_cmp=getdeviceinfo_4['artist']
        currentstatus_album_cmp=getdeviceinfo_4['album']
        currentstatus_url_cmp=getdeviceinfo_4['url']
        
        if (currentstatus_name==currentstatus_name_cmp)and(currentstatus_artist==currentstatus_artis_cmp)and(currentstatus_album==currentstatus_album_cmp)and(currentstatus_url==currentstatus_url_cmp):
            logging_save.logging.info('正常切换到下一首歌曲')
            #print '正常切换到下一首歌曲'
            
        else:
            logging_save.logging.warning('FAIL,FAIL,FAIL, 没有正常切换到下一首') 
            #print 'Fail, 没有正常切换到下一首'
        time.sleep(5)
    
    def pre_music(self):
        logging_save.logging.info('切换到上一首曲目，请等待。。。')
        #print '切换到上一首曲目，请等待。。。'
        postbody_next='{"command":"key","value":8}'  
        self.Connection.request('POST', '/api/key',postbody_next,self.Headers)
        self.Connection.close()
        self.Connection.request('GET', '/api/request-dev')
        getinformation_4=self.Connection.getresponse()
        readinformation_4=getinformation_4.read()
        #print readinformation_3
        time.sleep(5)
        
        getdeviceinfo_4=eval(readinformation_4)
        currentstatus_name=getdeviceinfo_4['name']
        currentstatus_artist=getdeviceinfo_4['artist']
        currentstatus_album=getdeviceinfo_4['album']
        currentstatus_url=getdeviceinfo_4['url']
        
        #先返回到上一步，然后再next到原来曲目，然后对比Get获取的关键信息
        postbody_before='{"command":"key","value":8}'  
        self.Connection.request('POST', '/api/key',postbody_before,self.Headers)
        self.Connection.close()
        time.sleep(5)
        
        postbody_before='{"command":"key","value":7}'  
        self.Connection.request('POST', '/api/key',postbody_before,self.Headers)
        self.Connection.close()
        time.sleep(5)
        
        self.Connection.request('GET', '/api/request-dev')
        getinformation_4=self.Connection.getresponse()
        readinformation_4=getinformation_4.read()
        #print readinformation_4
        
        getdeviceinfo_4=eval(readinformation_4)
        currentstatus_name_cmp=getdeviceinfo_4['name']
        currentstatus_artis_cmp=getdeviceinfo_4['artist']
        currentstatus_album_cmp=getdeviceinfo_4['album']
        currentstatus_url_cmp=getdeviceinfo_4['url']
        
        if (currentstatus_name==currentstatus_name_cmp)and(currentstatus_artist==currentstatus_artis_cmp)and(currentstatus_album==currentstatus_album_cmp)and(currentstatus_url==currentstatus_url_cmp):
            logging_save.logging.info('Result：正常切换到上一首歌曲')
            #print 'Result：正常切换到上一首歌曲'
            
        else:
            logging_save.logging.warning('Fail, 没有正常切换到下一首') 
            print 'FAIL,FAIL,FAIL,, 没有正常切换到上一首'
        time.sleep(5)

class  Kala_music(Play_pasuse_next_pre):
    def playurl(self):
        postbody={"command":"pb-start-url",
                "value": "http://image.kaolafm.net/mz/audios/201408/e6191927-1509-4cbd-b90b-0c741373ee31.mp3?appid=marxc4577&deviceid=100000029165298&audioid=1000000326510", 
                "title":"", 
                "artist":"",
                "album": "", 
                "mtype":5
                }
        PlaybackURLJson=json.dumps(postbody)
        self.Connection.request('POST', '/api/pb-start-url',PlaybackURLJson,self.Headers)
        self.Connection.close()
        logging_save.logging.info('~~~~此时播放已经切换到考拉音乐。。。。')
        #print '~~~~此时播放已经切换到考拉音乐。。。。'
        time.sleep(5)
        
class  ximalaya_music(Play_pasuse_next_pre):
    def playurl(self):
        postbody={"command":"pb-start-url",
                "value": "http://fdfs.xmcdn.com/group4/M04/79/36/wKgDs1PGpw-gd7xFAEqrTYQzId8968.mp3", 
                "title":"", 
                "artist":"",
                "album": "", 
                "mtype":8
                }
        PlaybackURLJson=json.dumps(postbody)
        self.Connection.request('POST', '/api/pb-start-url',PlaybackURLJson,self.Headers)
        self.Connection.close()
        logging_save.logging.info('~~~~此时播放已经切换到喜马拉雅音乐。。。。')
        #print '~~~~此时播放已经切换到喜马拉雅音乐。。。。'
        time.sleep(5)
        
if  __name__ == '__main__':
    playback=Play_pasuse_next_pre()
    playback.pause()
    playback.next_music()
    playback.pre_music()
    kala=Kala_music()
    kala.pause()
    kala.playurl()
    kala.next_music()
    kala.pre_music()
    ximalaya=ximalaya_music()
    ximalaya.pause()
    ximalaya.playurl()
    ximalaya.next_music()
    ximalaya.pre_music()
    
    